########################################################################
######### Pre-processes vertex and dyadic covariates for TERGM #########
########################################################################

########################################################################
# Install packages
detach(package:xergm, unload=T)
detach(package:statnet, unload=T)
install.packages("igraph")
########################################################################

# Set the working directory
setwd("~/Dropbox/ISQ_ms/5 Final/Replication")

# set savedir
SAVEDIR <- "~/Dropbox/ISQ_ms/5 Final/Replication"

# load libraries
library(igraph)

# load data

# read in state IDs data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs1970 <- stateIDs$stateabb[stateIDs$year==1970]
stateIDs1975 <- stateIDs$stateabb[stateIDs$year==1975]
stateIDs1980 <- stateIDs$stateabb[stateIDs$year==1980]
stateIDs1985 <- stateIDs$stateabb[stateIDs$year==1985]
stateIDs1990 <- stateIDs$stateabb[stateIDs$year==1990]
stateIDs1995 <- stateIDs$stateabb[stateIDs$year==1995]
stateIDs2000 <- stateIDs$stateabb[stateIDs$year==2000]
stateIDs2005 <- stateIDs$stateabb[stateIDs$year==2005]
stateIDs2010 <- stateIDs$stateabb[stateIDs$year==2010]

# read in adjacency matrices
dipcon1970 <- read.csv("DIPCON1970_3.0.csv",row.names=1) # 134 states
dipcon1975 <- read.csv("DIPCON1975_3.0.csv",row.names=1) # 148 states
dipcon1980 <- read.csv("DIPCON1980_3.0.csv",row.names=1) # 157 states
dipcon1985 <- read.csv("DIPCON1985_3.0.csv",row.names=1) # 162 states
dipcon1990 <- read.csv("DIPCON1990_3.0.csv",row.names=1) # 164 states
dipcon1995 <- read.csv("DIPCON1995_3.0.csv",row.names=1) # 183 states
dipcon2000 <- read.csv("DIPCON2000_3.0.csv",row.names=1) # 187 states
dipcon2005 <- read.csv("DIPCON2005_3.0.csv",row.names=1) # 188 states
dipcon2010 <- read.csv("DIPCON2010_3.0.csv",row.names=1) # 190 states

# create diplomatic networks
g1970 <- graph.adjacency(as.matrix(dipcon1970), mode="directed",weighted=NULL)
g1975 <- graph.adjacency(as.matrix(dipcon1975), mode="directed",weighted=NULL)
g1980 <- graph.adjacency(as.matrix(dipcon1980), mode="directed",weighted=NULL)
g1985 <- graph.adjacency(as.matrix(dipcon1985), mode="directed",weighted=NULL)
g1990 <- graph.adjacency(as.matrix(dipcon1990), mode="directed",weighted=NULL)
g1995 <- graph.adjacency(as.matrix(dipcon1995), mode="directed",weighted=NULL)
g2000 <- graph.adjacency(as.matrix(dipcon2000), mode="directed",weighted=NULL)
g2005 <- graph.adjacency(as.matrix(dipcon2005), mode="directed",weighted=NULL)
g2010 <- graph.adjacency(as.matrix(dipcon2010), mode="directed",weighted=NULL)

# read in vertex covariates
vcov <- read.csv("vcov.csv",stringsAsFactors=F)
cov1970 <- vcov[vcov$year==1970,]
cov1975 <- vcov[vcov$year==1975,]
cov1980 <- vcov[vcov$year==1980,]
cov1985 <- vcov[vcov$year==1985,]
cov1990 <- vcov[vcov$year==1990,]
cov1995 <- vcov[vcov$year==1995,]
cov2000 <- vcov[vcov$year==2000,]
cov2005 <- vcov[vcov$year==2005,]
cov2010 <- vcov[vcov$year==2010,]

# read in dyadic covariates; create networks
ally1970 <- read.csv("ally1970.csv", header=T,row.names=1,check.names=F)
ally1975 <- read.csv("ally1975.csv", header=T,row.names=1,check.names=F)
ally1980 <- read.csv("ally1980.csv", header=T,row.names=1,check.names=F)
ally1985 <- read.csv("ally1985.csv", header=T,row.names=1,check.names=F)
ally1990 <- read.csv("ally1990.csv", header=T,row.names=1,check.names=F)
ally1995 <- read.csv("ally1995.csv", header=T,row.names=1,check.names=F)
ally2000 <- read.csv("ally2000.csv", header=T,row.names=1,check.names=F)
ally2003 <- read.csv("ally2003.csv", header=T,row.names=1,check.names=F)

ally1970 <- graph.adjacency(as.matrix(ally1970), mode="undirected",weighted=NULL)
ally1975 <- graph.adjacency(as.matrix(ally1975), mode="undirected",weighted=NULL)
ally1980 <- graph.adjacency(as.matrix(ally1980), mode="undirected",weighted=NULL)
ally1985 <- graph.adjacency(as.matrix(ally1985), mode="undirected",weighted=NULL)
ally1990 <- graph.adjacency(as.matrix(ally1990), mode="undirected",weighted=NULL)
ally1995 <- graph.adjacency(as.matrix(ally1995), mode="undirected",weighted=NULL)
ally2000 <- graph.adjacency(as.matrix(ally2000), mode="undirected",weighted=NULL)
ally2003 <- graph.adjacency(as.matrix(ally2003), mode="undirected",weighted=NULL)

cont1970 <- read.csv("cont1970.csv", header=T,row.names=1,check.names=F)
cont1975 <- read.csv("cont1975.csv", header=T,row.names=1,check.names=F)
cont1980 <- read.csv("cont1980.csv", header=T,row.names=1,check.names=F)
cont1985 <- read.csv("cont1985.csv", header=T,row.names=1,check.names=F)
cont1990 <- read.csv("cont1990.csv", header=T,row.names=1,check.names=F)
cont1995 <- read.csv("cont1995.csv", header=T,row.names=1,check.names=F)
cont2000 <- read.csv("cont2000.csv", header=T,row.names=1,check.names=F)
cont2005 <- read.csv("cont2005.csv", header=T,row.names=1,check.names=F)

cont1970 <- graph.adjacency(as.matrix(cont1970), mode="undirected",weighted=NULL)
cont1975 <- graph.adjacency(as.matrix(cont1975), mode="undirected",weighted=NULL)
cont1980 <- graph.adjacency(as.matrix(cont1980), mode="undirected",weighted=NULL)
cont1985 <- graph.adjacency(as.matrix(cont1985), mode="undirected",weighted=NULL)
cont1990 <- graph.adjacency(as.matrix(cont1990), mode="undirected",weighted=NULL)
cont1995 <- graph.adjacency(as.matrix(cont1995), mode="undirected",weighted=NULL)
cont2000 <- graph.adjacency(as.matrix(cont2000), mode="undirected",weighted=NULL)
cont2005 <- graph.adjacency(as.matrix(cont2005), mode="undirected",weighted=NULL)

trade1970 <- read.csv("trade1970.csv", header=T,row.names=1,check.names=F)
trade1975 <- read.csv("trade1975.csv", header=T,row.names=1,check.names=F)
trade1980 <- read.csv("trade1980.csv", header=T,row.names=1,check.names=F)
trade1985 <- read.csv("trade1985.csv", header=T,row.names=1,check.names=F)
trade1990 <- read.csv("trade1990.csv", header=T,row.names=1,check.names=F)
trade1995 <- read.csv("trade1995.csv", header=T,row.names=1,check.names=F)
trade2000 <- read.csv("trade2000.csv", header=T,row.names=1,check.names=F)
trade2005 <- read.csv("trade2005.csv", header=T,row.names=1,check.names=F)

trade1970 <- graph.adjacency(as.matrix(trade1970), mode="directed",weighted=T)
trade1975 <- graph.adjacency(as.matrix(trade1975), mode="directed",weighted=T)
trade1980 <- graph.adjacency(as.matrix(trade1980), mode="directed",weighted=T)
trade1985 <- graph.adjacency(as.matrix(trade1985), mode="directed",weighted=T)
trade1990 <- graph.adjacency(as.matrix(trade1990), mode="directed",weighted=T)
trade1995 <- graph.adjacency(as.matrix(trade1995), mode="directed",weighted=T)
trade2000 <- graph.adjacency(as.matrix(trade2000), mode="directed",weighted=T)
trade2005 <- graph.adjacency(as.matrix(trade2005), mode="directed",weighted=T)


########################################################################
################# Model 1: polity but not HRs or EF ####################
########################################################################

# extract variables of interest
mod1_vars <- c("stateabb", "ccode", "year",
               "log_pop", "log_realgdp", "log_rgdppc",
               "log_cinc", "log_milex", "log_milper", "nw", 
               "polity", "dem_dum",
               "sub_region_n", "sub_region_c", "macro_region_n", "macro_region_c",
               "igo_hq")

mod1_vcov <- vcov[names(vcov) %in% mod1_vars]

# Drop missing values
good <- complete.cases(mod1_vcov)
mod1_vcov <- mod1_vcov[good,] 

# save new vcovs
write.csv(mod1_vcov, file=file.path(SAVEDIR, "mod1_vcov.csv"), row.names=F)


##### Dyadic data

### 1970

# identify nodes with NAs
v_nas1970 <- stateIDs1970[!stateIDs1970 %in% mod1_vcov$stateabb[mod1_vcov$year==1970]] # 8 NAs

# delete them from diplomatic network; save new edge list
mod1_g1970 <- delete.vertices(g1970, v=v_nas1970)
mod1_g1970 # 126 nodes, 3,462 edges
mod1_el1970 <- get.edgelist(mod1_g1970)
write.csv(mod1_el1970, "mod1_el1970.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod1_ally1970 <- delete.vertices(ally1970, v=v_nas1970)
mod1_ally1970 # 126 nodes, 526 edges
mod1_ally1970 <- get.adjacency(mod1_ally1970, type="both")
write.csv(as.matrix(mod1_ally1970), "mod1_ally1970.csv")

# delete them from contiguity network; save new adjacency matrix
mod1_cont1970 <- delete.vertices(cont1970, v=v_nas1970)
mod1_cont1970 # 126 nodes, 208 edges
mod1_cont1970 <- get.adjacency(mod1_cont1970, type="both")
write.csv(as.matrix(mod1_cont1970), "mod1_cont1970.csv")

# delete them from trade network; save new adjacency matrix
mod1_trade1970 <- delete.vertices(trade1970, v=v_nas1970)
mod1_trade1970 # 126 nodes, 8,966 edges
mod1_trade1970 <- get.adjacency(mod1_trade1970, attr="weight")
write.csv(as.matrix(mod1_trade1970), "mod1_trade1970.csv")


### 1975

# identify nodes with NAs
v_nas1975 <- stateIDs1975[!stateIDs1975 %in% mod1_vcov$stateabb[mod1_vcov$year==1975]] # 10 NAs

# delete them from diplomatic network; save new edge list
mod1_g1975 <- delete.vertices(g1975, v=v_nas1975)
mod1_g1975 # 138 nodes, 4,232 edges
mod1_el1975 <- get.edgelist(mod1_g1975)
write.csv(mod1_el1975, "mod1_el1975.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod1_ally1975 <- delete.vertices(ally1975, v=v_nas1975)
mod1_ally1975 # 138 nodes, 891 edges
mod1_ally1975 <- get.adjacency(mod1_ally1975, type="both")
write.csv(as.matrix(mod1_ally1975), "mod1_ally1975.csv")

# delete them from contiguity network; save new adjacency matrix
mod1_cont1975 <- delete.vertices(cont1975, v=v_nas1975)
mod1_cont1975 # 138 nodes, 244 edges
mod1_cont1975 <- get.adjacency(mod1_cont1975, type="both")
write.csv(as.matrix(mod1_cont1975), "mod1_cont1975.csv")

# delete them from trade network; save new adjacency matrix
mod1_trade1975 <- delete.vertices(trade1975, v=v_nas1975)
mod1_trade1975 # 138 nodes, 10,786 edges
mod1_trade1975 <- get.adjacency(mod1_trade1975, attr="weight")
write.csv(as.matrix(mod1_trade1975), "mod1_trade1975.csv")


### 1980

# identify nodes with NAs
v_nas1980 <- stateIDs1980[!stateIDs1980 %in% mod1_vcov$stateabb[mod1_vcov$year==1980]] # 21 NAs

# delete them from diplomatic network; save new edge list
mod1_g1980 <- delete.vertices(g1980, v=v_nas1980)
mod1_g1980 # 136 nodes, 4,769 edges
mod1_el1980 <- get.edgelist(mod1_g1980)
write.csv(mod1_el1980, "mod1_el1980.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod1_ally1980 <- delete.vertices(ally1980, v=v_nas1980)
mod1_ally1980 # 136 nodes, 986 edges
mod1_ally1980 <- get.adjacency(mod1_ally1980, type="both")
write.csv(as.matrix(mod1_ally1980), "mod1_ally1980.csv")

# delete them from contiguity network; save new adjacency matrix
mod1_cont1980 <- delete.vertices(cont1980, v=v_nas1980)
mod1_cont1980 # 136 nodes, 227 edges
mod1_cont1980 <- get.adjacency(mod1_cont1980, type="both")
write.csv(as.matrix(mod1_cont1980), "mod1_cont1980.csv")

# delete them from trade network; save new adjacency matrix
mod1_trade1980 <- delete.vertices(trade1980, v=v_nas1980)
mod1_trade1980 # 136 nodes, 11,156 edges
mod1_trade1980 <- get.adjacency(mod1_trade1980, attr="weight")
write.csv(as.matrix(mod1_trade1980), "mod1_trade1980.csv")


### 1985

# identify nodes with NAs
v_nas1985 <- stateIDs1985[!stateIDs1985 %in% mod1_vcov$stateabb[mod1_vcov$year==1985]] # 24 NAs

# delete them from diplomatic network; save new edge list
mod1_g1985 <- delete.vertices(g1985, v=v_nas1985)
mod1_g1985 # 138 nodes, 5,114 edges
mod1_el1985 <- get.edgelist(mod1_g1985)
write.csv(mod1_el1985, "mod1_el1985.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod1_ally1985 <- delete.vertices(ally1985, v=v_nas1985)
mod1_ally1985 # 138 nodes, 1,039 edges
mod1_ally1985 <- get.adjacency(mod1_ally1985, type="both")
write.csv(as.matrix(mod1_ally1985), "mod1_ally1985.csv")

# delete them from contiguity network; save new adjacency matrix
mod1_cont1985 <- delete.vertices(cont1985, v=v_nas1985)
mod1_cont1985 # 138 nodes, 230 edges
mod1_cont1985 <- get.adjacency(mod1_cont1985, type="both")
write.csv(as.matrix(mod1_cont1985), "mod1_cont1985.csv")

# delete them from trade network; save new adjacency matrix
mod1_trade1985 <- delete.vertices(trade1985, v=v_nas1985)
mod1_trade1985 # 138 nodes, 11,590 edges
mod1_trade1985 <- get.adjacency(mod1_trade1985, attr="weight")
write.csv(as.matrix(mod1_trade1985), "mod1_trade1985.csv")


### 1990

# identify nodes with NAs
v_nas1990 <- stateIDs1990[!stateIDs1990 %in% mod1_vcov$stateabb[mod1_vcov$year==1990]] # 32 NAs

# delete them from diplomatic network; save new edge list
mod1_g1990 <- delete.vertices(g1990, v=v_nas1990)
mod1_g1990 # 132 nodes, 4,822 edges
mod1_el1990 <- get.edgelist(mod1_g1990)
write.csv(mod1_el1990, "mod1_el1990.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod1_ally1990 <- delete.vertices(ally1990, v=v_nas1990)
mod1_ally1990 # 132 nodes, 919 edges
mod1_ally1990 <- get.adjacency(mod1_ally1990, type="both")
write.csv(as.matrix(mod1_ally1990), "mod1_ally1990.csv")

# delete them from contiguity network; save new adjacency matrix
mod1_cont1990 <- delete.vertices(cont1990, v=v_nas1990)
mod1_cont1990 # 132 nodes, 219 edges
mod1_cont1990 <- get.adjacency(mod1_cont1990, type="both")
write.csv(as.matrix(mod1_cont1990), "mod1_cont1990.csv")

# delete them from trade network; save new adjacency matrix
mod1_trade1990 <- delete.vertices(trade1990, v=v_nas1990)
mod1_trade1990 # 132 nodes, 11,586 edges
mod1_trade1990 <- get.adjacency(mod1_trade1990, attr="weight")
write.csv(as.matrix(mod1_trade1990), "mod1_trade1990.csv")


### 1995

# identify nodes with NAs
v_nas1995 <- stateIDs1995[!stateIDs1995 %in% mod1_vcov$stateabb[mod1_vcov$year==1995]] # 24 NAs

# delete them from diplomatic network; save new edge list
mod1_g1995 <- delete.vertices(g1995, v=v_nas1995)
mod1_g1995 # 159 nodes, 5,699 edges
mod1_el1995 <- get.edgelist(mod1_g1995)
write.csv(mod1_el1995, "mod1_el1995.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod1_ally1995 <- delete.vertices(ally1995, v=v_nas1995)
mod1_ally1995 # 159 nodes, 1,686 edges
mod1_ally1995 <- get.adjacency(mod1_ally1995, type="both")
write.csv(as.matrix(mod1_ally1995), "mod1_ally1995.csv")

# delete them from contiguity network; save new adjacency matrix
mod1_cont1995 <- delete.vertices(cont1995, v=v_nas1995)
mod1_cont1995 # 159 nodes, 289 edges
mod1_cont1995 <- get.adjacency(mod1_cont1995, type="both")
write.csv(as.matrix(mod1_cont1995), "mod1_cont1995.csv")

# delete them from trade network; save new adjacency matrix
mod1_trade1995 <- delete.vertices(trade1995, v=v_nas1995)
mod1_trade1995 # 159 nodes, 16,672 edges
mod1_trade1995 <- get.adjacency(mod1_trade1995, attr="weight")
write.csv(as.matrix(mod1_trade1995), "mod1_trade1995.csv")


### 2000

# identify nodes with NAs
v_nas2000 <- stateIDs2000[!stateIDs2000 %in% mod1_vcov$stateabb[mod1_vcov$year==2000]] # 34 NAs

# delete them from diplomatic network; save new edge list
mod1_g2000 <- delete.vertices(g2000, v=v_nas2000)
mod1_g2000 # 153 nodes, 5,910 edges
mod1_el2000 <- get.edgelist(mod1_g2000)
write.csv(mod1_el2000, "mod1_el2000.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod1_ally2000 <- delete.vertices(ally2000, v=v_nas2000)
mod1_ally2000 # 153 nodes, 1,596 edges
mod1_ally2000 <- get.adjacency(mod1_ally2000, type="both")
write.csv(as.matrix(mod1_ally2000), "mod1_ally2000.csv")

# delete them from contiguity network; save new adjacency matrix
mod1_cont2000 <- delete.vertices(cont2000, v=v_nas2000)
mod1_cont2000 # 153 nodes, 290 edges
mod1_cont2000 <- get.adjacency(mod1_cont2000, type="both")
write.csv(as.matrix(mod1_cont2000), "mod1_cont2000.csv")

# delete them from trade network; save new adjacency matrix
mod1_trade2000 <- delete.vertices(trade2000, v=v_nas2000)
mod1_trade2000 # 153 nodes, 16,740 edges
mod1_trade2000 <- get.adjacency(mod1_trade2000, attr="weight")
write.csv(as.matrix(mod1_trade2000), "mod1_trade2000.csv")


### 2005

# identify nodes with NAs
v_nas2005 <- stateIDs2005[!stateIDs2005 %in% mod1_vcov$stateabb[mod1_vcov$year==2005]] # 34 NAs

# delete them from diplomatic network; save new edge list
mod1_g2005 <- delete.vertices(g2005, v=v_nas2005)
mod1_g2005 # 154 nodes, 6,665 edges
mod1_el2005 <- get.edgelist(mod1_g2005)
write.csv(mod1_el2005, "mod1_el2005.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod1_ally2003 <- delete.vertices(ally2003, v=v_nas2005)
mod1_ally2003 # 154 nodes, 1,671 edges
mod1_ally2003 <- get.adjacency(mod1_ally2003, type="both")
write.csv(as.matrix(mod1_ally2003), "mod1_ally2003.csv")

# delete them from contiguity network; save new adjacency matrix
mod1_cont2005 <- delete.vertices(cont2005, v=v_nas2005)
mod1_cont2005 # 154 nodes, 274 edges
mod1_cont2005 <- get.adjacency(mod1_cont2005, type="both")
write.csv(as.matrix(mod1_cont2005), "mod1_cont2005.csv")

# delete them from trade network; save new adjacency matrix
mod1_trade2005 <- delete.vertices(trade2005, v=v_nas2005)
mod1_trade2005 # 154 nodes, 17,080 edges
mod1_trade2005 <- get.adjacency(mod1_trade2005, attr="weight")
write.csv(as.matrix(mod1_trade2005), "mod1_trade2005.csv")


########################################################################
################# Model 2: HRs but not polity or EF ####################
########################################################################

# extract variables of interest
mod2_vars <- c("stateabb", "ccode", "year",
               "log_pop", "log_realgdp", "log_rgdppc",
               "log_cinc", "log_milex", "log_milper", "nw", 
               "pts_inv", "pts_dum",
               "sub_region_n", "sub_region_c", "macro_region_n", "macro_region_c",
               "igo_hq")

mod2_vcov <- vcov[names(vcov) %in% mod2_vars]

# Drop missing values
good <- complete.cases(mod2_vcov)
mod2_vcov <- mod2_vcov[good,] 

# save new vcovs
write.csv(mod2_vcov, file=file.path(SAVEDIR, "mod2_vcov.csv"), row.names=F)


##### Dyadic data

### 1980

# identify nodes with NAs
v_nas1980 <- stateIDs1980[!stateIDs1980 %in% mod2_vcov$stateabb[mod2_vcov$year==1980]] # 21 NAs

# delete them from diplomatic network; save new edge list
mod2_g1980 <- delete.vertices(g1980, v=v_nas1980)
mod2_g1980 # 136 nodes, 4,436 edges
mod2_el1980 <- get.edgelist(mod2_g1980)
write.csv(mod2_el1980, "mod2_el1980.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod2_ally1980 <- delete.vertices(ally1980, v=v_nas1980)
mod2_ally1980 # 136 nodes, 977 edges
mod2_ally1980 <- get.adjacency(mod2_ally1980, type="both")
write.csv(as.matrix(mod2_ally1980), "mod2_ally1980.csv")

# delete them from contiguity network; save new adjacency matrix
mod2_cont1980 <- delete.vertices(cont1980, v=v_nas1980)
mod2_cont1980 # 136 nodes, 213 edges
mod2_cont1980 <- get.adjacency(mod2_cont1980, type="both")
write.csv(as.matrix(mod2_cont1980), "mod2_cont1980.csv")

# delete them from trade network; save new adjacency matrix
mod2_trade1980 <- delete.vertices(trade1980, v=v_nas1980)
mod2_trade1980 # 136 nodes, 10,872 edges
mod2_trade1980 <- get.adjacency(mod2_trade1980, attr="weight")
write.csv(as.matrix(mod2_trade1980), "mod2_trade1980.csv")


### 1985

# identify nodes with NAs
v_nas1985 <- stateIDs1985[!stateIDs1985 %in% mod2_vcov$stateabb[mod2_vcov$year==1985]] # 20 NAs

# delete them from diplomatic network; save new edge list
mod2_g1985 <- delete.vertices(g1985, v=v_nas1985)
mod2_g1985 # 142 nodes, 4,899 edges
mod2_el1985 <- get.edgelist(mod2_g1985)
write.csv(mod2_el1985, "mod2_el1985.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod2_ally1985 <- delete.vertices(ally1985, v=v_nas1985)
mod2_ally1985 # 142 nodes, 1,067 edges
mod2_ally1985 <- get.adjacency(mod2_ally1985, type="both")
write.csv(as.matrix(mod2_ally1985), "mod2_ally1985.csv")

# delete them from contiguity network; save new adjacency matrix
mod2_cont1985 <- delete.vertices(cont1985, v=v_nas1985)
mod2_cont1985 # 142 nodes, 235 edges
mod2_cont1985 <- get.adjacency(mod2_cont1985, type="both")
write.csv(as.matrix(mod2_cont1985), "mod2_cont1985.csv")

# delete them from trade network; save new adjacency matrix
mod2_trade1985 <- delete.vertices(trade1985, v=v_nas1985)
mod2_trade1985 # 142 nodes, 11,680 edges
mod2_trade1985 <- get.adjacency(mod2_trade1985, attr="weight")
write.csv(as.matrix(mod2_trade1985), "mod2_trade1985.csv")


### 1990

# identify nodes with NAs
v_nas1990 <- stateIDs1990[!stateIDs1990 %in% mod2_vcov$stateabb[mod2_vcov$year==1990]] # 27 NAs

# delete them from diplomatic network; save new edge list
mod2_g1990 <- delete.vertices(g1990, v=v_nas1990)
mod2_g1990 # 137 nodes, 4,763 edges
mod2_el1990 <- get.edgelist(mod2_g1990)
write.csv(mod2_el1990, "mod2_el1990.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod2_ally1990 <- delete.vertices(ally1990, v=v_nas1990)
mod2_ally1990 # 137 nodes, 960 edges
mod2_ally1990 <- get.adjacency(mod2_ally1990, type="both")
write.csv(as.matrix(mod2_ally1990), "mod2_ally1990.csv")

# delete them from contiguity network; save new adjacency matrix
mod2_cont1990 <- delete.vertices(cont1990, v=v_nas1990)
mod2_cont1990 # 137 nodes, 226 edges
mod2_cont1990 <- get.adjacency(mod2_cont1990, type="both")
write.csv(as.matrix(mod2_cont1990), "mod2_cont1990.csv")

# delete them from trade network; save new adjacency matrix
mod2_trade1990 <- delete.vertices(trade1990, v=v_nas1990)
mod2_trade1990 # 137 nodes, 12,042 edges
mod2_trade1990 <- get.adjacency(mod2_trade1990, attr="weight")
write.csv(as.matrix(mod2_trade1990), "mod2_trade1990.csv")


### 1995

# identify nodes with NAs
v_nas1995 <- stateIDs1995[!stateIDs1995 %in% mod2_vcov$stateabb[mod2_vcov$year==1995]] # 16 NAs

# delete them from diplomatic network; save new edge list
mod2_g1995 <- delete.vertices(g1995, v=v_nas1995)
mod2_g1995 # 167 nodes, 5,619 edges
mod2_el1995 <- get.edgelist(mod2_g1995)
write.csv(mod2_el1995, "mod2_el1995.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod2_ally1995 <- delete.vertices(ally1995, v=v_nas1995)
mod2_ally1995 # 167 nodes, 1,676 edges
mod2_ally1995 <- get.adjacency(mod2_ally1995, type="both")
write.csv(as.matrix(mod2_ally1995), "mod2_ally1995.csv")

# delete them from contiguity network; save new adjacency matrix
mod2_cont1995 <- delete.vertices(cont1995, v=v_nas1995)
mod2_cont1995 # 167 nodes, 299 edges
mod2_cont1995 <- get.adjacency(mod2_cont1995, type="both")
write.csv(as.matrix(mod2_cont1995), "mod2_cont1995.csv")

# delete them from trade network; save new adjacency matrix
mod2_trade1995 <- delete.vertices(trade1995, v=v_nas1995)
mod2_trade1995 # 167 nodes, 17,282 edges
mod2_trade1995 <- get.adjacency(mod2_trade1995, attr="weight")
write.csv(as.matrix(mod2_trade1995), "mod2_trade1995.csv")


### 2000

# identify nodes with NAs
v_nas2000 <- stateIDs2000[!stateIDs2000 %in% mod2_vcov$stateabb[mod2_vcov$year==2000]] # 20 NAs

# delete them from diplomatic network; save new edge list
mod2_g2000 <- delete.vertices(g2000, v=v_nas2000)
mod2_g2000 # 167 nodes, 6,054 edges
mod2_el2000 <- get.edgelist(mod2_g2000)
write.csv(mod2_el2000, "mod2_el2000.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod2_ally2000 <- delete.vertices(ally2000, v=v_nas2000)
mod2_ally2000 # 167 nodes, 1,688 edges
mod2_ally2000 <- get.adjacency(mod2_ally2000, type="both")
write.csv(as.matrix(mod2_ally2000), "mod2_ally2000.csv")

# delete them from contiguity network; save new adjacency matrix
mod2_cont2000 <- delete.vertices(cont2000, v=v_nas2000)
mod2_cont2000 # 167 nodes, 314 edges
mod2_cont2000 <- get.adjacency(mod2_cont2000, type="both")
write.csv(as.matrix(mod2_cont2000), "mod2_cont2000.csv")

# delete them from trade network; save new adjacency matrix
mod2_trade2000 <- delete.vertices(trade2000, v=v_nas2000)
mod2_trade2000 # 167 nodes, 18,458 edges
mod2_trade2000 <- get.adjacency(mod2_trade2000, attr="weight")
write.csv(as.matrix(mod2_trade2000), "mod2_trade2000.csv")


### 2005

# identify nodes with NAs
v_nas2005 <- stateIDs2005[!stateIDs2005 %in% mod2_vcov$stateabb[mod2_vcov$year==2005]] # 24 NAs

# delete them from diplomatic network; save new edge list
mod2_g2005 <- delete.vertices(g2005, v=v_nas2005)
mod2_g2005 # 164 nodes, 6,732 edges
mod2_el2005 <- get.edgelist(mod2_g2005)
write.csv(mod2_el2005, "mod2_el2005.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod2_ally2003 <- delete.vertices(ally2003, v=v_nas2005)
mod2_ally2003 # 164 nodes, 1,798 edges
mod2_ally2003 <- get.adjacency(mod2_ally2003, type="both")
write.csv(as.matrix(mod2_ally2003), "mod2_ally2003.csv")

# delete them from contiguity network; save new adjacency matrix
mod2_cont2005 <- delete.vertices(cont2005, v=v_nas2005)
mod2_cont2005 # 164 nodes, 297 edges
mod2_cont2005 <- get.adjacency(mod2_cont2005, type="both")
write.csv(as.matrix(mod2_cont2005), "mod2_cont2005.csv")

# delete them from trade network; save new adjacency matrix
mod2_trade2005 <- delete.vertices(trade2005, v=v_nas2005)
mod2_trade2005 # 164 nodes, 18,692 edges
mod2_trade2005 <- get.adjacency(mod2_trade2005, attr="weight")
write.csv(as.matrix(mod2_trade2005), "mod2_trade2005.csv")


########################################################################
################# Model 3: EF but not polity or HRs ####################
########################################################################

# extract variables of interest
mod3_vars <- c("stateabb", "ccode", "year",
               "log_pop", "log_realgdp", "log_rgdppc",
               "log_cinc", "log_milex", "log_milper", "nw", 
               "hf_efiscore", "ief_dum",
               "sub_region_n", "sub_region_c", "macro_region_n", "macro_region_c",
               "igo_hq")

mod3_vcov <- vcov[names(vcov) %in% mod3_vars]

# Drop missing values
good <- complete.cases(mod3_vcov)
mod3_vcov <- mod3_vcov[good,] 

# save new vcovs
write.csv(mod3_vcov, file=file.path(SAVEDIR, "mod3_vcov.csv"), row.names=F)


##### Dyadic data

### 1995

# identify nodes with NAs
v_nas1995 <- stateIDs1995[!stateIDs1995 %in% mod3_vcov$stateabb[mod3_vcov$year==1995]] # 43 NAs

# delete them from diplomatic network; save new edge list
mod3_g1995 <- delete.vertices(g1995, v=v_nas1995)
mod3_g1995 # 140 nodes, 5,418 edges
mod3_el1995 <- get.edgelist(mod3_g1995)
write.csv(mod3_el1995, "mod3_el1995.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod3_ally1995 <- delete.vertices(ally1995, v=v_nas1995)
mod3_ally1995 # 140 nodes, 1,436 edges
mod3_ally1995 <- get.adjacency(mod3_ally1995, type="both")
write.csv(as.matrix(mod3_ally1995), "mod3_ally1995.csv")

# delete them from contiguity network; save new adjacency matrix
mod3_cont1995 <- delete.vertices(cont1995, v=v_nas1995)
mod3_cont1995 # 140 nodes, 242 edges
mod3_cont1995 <- get.adjacency(mod3_cont1995, type="both")
write.csv(as.matrix(mod3_cont1995), "mod3_cont1995.csv")

# delete them from trade network; save new adjacency matrix
mod3_trade1995 <- delete.vertices(trade1995, v=v_nas1995)
mod3_trade1995 # 140 nodes, 14,396 edges
mod3_trade1995 <- get.adjacency(mod3_trade1995, attr="weight")
write.csv(as.matrix(mod3_trade1995), "mod3_trade1995.csv")


### 2000

# identify nodes with NAs
v_nas2000 <- stateIDs2000[!stateIDs2000 %in% mod3_vcov$stateabb[mod3_vcov$year==2000]] # 40 NAs

# delete them from diplomatic network; save new edge list
mod3_g2000 <- delete.vertices(g2000, v=v_nas2000)
mod3_g2000 # 147 nodes, 5,795 edges
mod3_el2000 <- get.edgelist(mod3_g2000)
write.csv(mod3_el2000, "mod3_el2000.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod3_ally2000 <- delete.vertices(ally2000, v=v_nas2000)
mod3_ally2000 # 147 nodes, 1,558 edges
mod3_ally2000 <- get.adjacency(mod3_ally2000, type="both")
write.csv(as.matrix(mod3_ally2000), "mod3_ally2000.csv")

# delete them from contiguity network; save new adjacency matrix
mod3_cont2000 <- delete.vertices(cont2000, v=v_nas2000)
mod3_cont2000 # 147 nodes, 260 edges
mod3_cont2000 <- get.adjacency(mod3_cont2000, type="both")
write.csv(as.matrix(mod3_cont2000), "mod3_cont2000.csv")

# delete them from trade network; save new adjacency matrix
mod3_trade2000 <- delete.vertices(trade2000, v=v_nas2000)
mod3_trade2000 # 147 nodes, 15,974 edges
mod3_trade2000 <- get.adjacency(mod3_trade2000, attr="weight")
write.csv(as.matrix(mod3_trade2000), "mod3_trade2000.csv")


### 2005

# identify nodes with NAs
v_nas2005 <- stateIDs2005[!stateIDs2005 %in% mod3_vcov$stateabb[mod3_vcov$year==2005]] # 36 NAs

# delete them from diplomatic network; save new edge list
mod3_g2005 <- delete.vertices(g2005, v=v_nas2005)
mod3_g2005 # 152 nodes, 6,570 edges
mod3_el2005 <- get.edgelist(mod3_g2005)
write.csv(mod3_el2005, "mod3_el2005.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod3_ally2003 <- delete.vertices(ally2003, v=v_nas2005)
mod3_ally2003 # 152 nodes, 1,724 edges
mod3_ally2003 <- get.adjacency(mod3_ally2003, type="both")
write.csv(as.matrix(mod3_ally2003), "mod3_ally2003.csv")

# delete them from contiguity network; save new adjacency matrix
mod3_cont2005 <- delete.vertices(cont2005, v=v_nas2005)
mod3_cont2005 # 152 nodes, 261 edges
mod3_cont2005 <- get.adjacency(mod3_cont2005, type="both")
write.csv(as.matrix(mod3_cont2005), "mod3_cont2005.csv")

# delete them from trade network; save new adjacency matrix
mod3_trade2005 <- delete.vertices(trade2005, v=v_nas2005)
mod3_trade2005 # 152 nodes, 17,130 edges
mod3_trade2005 <- get.adjacency(mod3_trade2005, attr="weight")
write.csv(as.matrix(mod3_trade2005), "mod3_trade2005.csv")


########################################################################
################### Model 4: all fundamental values ####################
########################################################################

# extract variables of interest
mod4_vars <- c("stateabb", "ccode", "year",
               "log_pop", "log_realgdp", "log_rgdppc",
               "log_cinc", "log_milex", "log_milper", "nw",
               "polity", "dem_dum",
               "pts_inv", "pts_dum",
               "hf_efiscore", "ief_dum",
               "sub_region_n", "sub_region_c", "macro_region_n", "macro_region_c",
               "igo_hq")

mod4_vcov <- vcov[names(vcov) %in% mod4_vars]

# Drop missing values
good <- complete.cases(mod4_vcov)
mod4_vcov <- mod4_vcov[good,] 

# save new vcovs
write.csv(mod4_vcov, file=file.path(SAVEDIR, "mod4_vcov.csv"), row.names=F)


##### Dyadic data

### 1995

# identify nodes with NAs
v_nas1995 <- stateIDs1995[!stateIDs1995 %in% mod4_vcov$stateabb[mod4_vcov$year==1995]] # 50 NAs

# delete them from diplomatic network; save new edge list
mod4_g1995 <- delete.vertices(g1995, v=v_nas1995)
mod4_g1995 # 133 nodes, 4,850 edges
mod4_el1995 <- get.edgelist(mod4_g1995)
write.csv(mod4_el1995, "mod4_el1995.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod4_ally1995 <- delete.vertices(ally1995, v=v_nas1995)
mod4_ally1995 # 133 nodes, 1,302 edges
mod4_ally1995 <- get.adjacency(mod4_ally1995, type="both")
write.csv(as.matrix(mod4_ally1995), "mod4_ally1995.csv")

# delete them from contiguity network; save new adjacency matrix
mod4_cont1995 <- delete.vertices(cont1995, v=v_nas1995)
mod4_cont1995 # 133 nodes, 222 edges
mod4_cont1995 <- get.adjacency(mod4_cont1995, type="both")
write.csv(as.matrix(mod4_cont1995), "mod4_cont1995.csv")

# delete them from trade network; save new adjacency matrix
mod4_trade1995 <- delete.vertices(trade1995, v=v_nas1995)
mod4_trade1995 # 133 nodes, 13,094 edges
mod4_trade1995 <- get.adjacency(mod4_trade1995, attr="weight")
write.csv(as.matrix(mod4_trade1995), "mod4_trade1995.csv")


### 2000

# identify nodes with NAs
v_nas2000 <- stateIDs2000[!stateIDs2000 %in% mod4_vcov$stateabb[mod4_vcov$year==2000]] # 47 NAs

# delete them from diplomatic network; save new edge list
mod4_g2000 <- delete.vertices(g2000, v=v_nas2000)
mod4_g2000 # 140 nodes, 5,265 edges
mod4_el2000 <- get.edgelist(mod4_g2000)
write.csv(mod4_el2000, "mod4_el2000.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod4_ally2000 <- delete.vertices(ally2000, v=v_nas2000)
mod4_ally2000 # 140 nodes, 1,448 edges
mod4_ally2000 <- get.adjacency(mod4_ally2000, type="both")
write.csv(as.matrix(mod4_ally2000), "mod4_ally2000.csv")

# delete them from contiguity network; save new adjacency matrix
mod4_cont2000 <- delete.vertices(cont2000, v=v_nas2000)
mod4_cont2000 # 140 nodes, 248 edges
mod4_cont2000 <- get.adjacency(mod4_cont2000, type="both")
write.csv(as.matrix(mod4_cont2000), "mod4_cont2000.csv")

# delete them from trade network; save new adjacency matrix
mod4_trade2000 <- delete.vertices(trade2000, v=v_nas2000)
mod4_trade2000 # 140 nodes, 14,796 edges
mod4_trade2000 <- get.adjacency(mod4_trade2000, attr="weight")
write.csv(as.matrix(mod4_trade2000), "mod4_trade2000.csv")


### 2005

# identify nodes with NAs
v_nas2005 <- stateIDs2005[!stateIDs2005 %in% mod4_vcov$stateabb[mod4_vcov$year==2005]] # 41 NAs

# delete them from diplomatic network; save new edge list
mod4_g2005 <- delete.vertices(g2005, v=v_nas2005)
mod4_g2005 # 147 nodes, 6,097 edges
mod4_el2005 <- get.edgelist(mod4_g2005)
write.csv(mod4_el2005, "mod4_el2005.csv", row.names=F)

# delete them from alliance network; save new adjacency matrix
mod4_ally2003 <- delete.vertices(ally2003, v=v_nas2005)
mod4_ally2003 # 147 nodes, 1,554 edges
mod4_ally2003 <- get.adjacency(mod4_ally2003, type="both")
write.csv(as.matrix(mod4_ally2003), "mod4_ally2003.csv")

# delete them from contiguity network; save new adjacency matrix
mod4_cont2005 <- delete.vertices(cont2005, v=v_nas2005)
mod4_cont2005 # 147 nodes, 250 edges
mod4_cont2005 <- get.adjacency(mod4_cont2005, type="both")
write.csv(as.matrix(mod4_cont2005), "mod4_cont2005.csv")

# delete them from trade network; save new adjacency matrix
mod4_trade2005 <- delete.vertices(trade2005, v=v_nas2005)
mod4_trade2005 # 147 nodes, 15,912 edges
mod4_trade2005 <- get.adjacency(mod4_trade2005, attr="weight")
write.csv(as.matrix(mod4_trade2005), "mod4_trade2005.csv")


### Online Appendix

### Summary Statistics

### Table A9. State-Level Variable Distributions for Model 1 (2000)

num_vcov2000 <- mod4_vcov[mod4_vcov$year==2000, 
                          c("log_rgdppc", "log_milex", "nw", "polity", "pts_inv", "hf_efiscore", "igo_hq")]

xtable(summary(num_vcov2000))


### Table A10. Correlation Matrix for State-Level Variables in Model 1 (2000)

xtable(cor(num_vcov2000[,], num_vcov2000[,]))

